package agh.gs.semdrools.alarm;

import java.rmi.RemoteException;
import java.util.List;

import org.drools.runtime.StatefulKnowledgeSession;

import agh.gs.semdrools.SessionProvider;
import agh.gs.semdrools.fact.Alarm;

import pl.edu.agh.msc.monitoring.metric.IAlarmSubscriber;
import pl.edu.agh.msc.monitoring.metric.IRunningMetric;
import pl.edu.agh.msc.monitoring.metric.RunningMetricImpl;


public class AlarmListener implements IAlarmSubscriber {
	
	public AlarmListener( SessionProvider sessionProvider ) {
		
		this.sessionProvider = sessionProvider;
		
	}

	
	@Override
	public void notifyNewAlarm( IRunningMetric alarmCause,
	                            List< IRunningMetric > metrics,
	                            String description ) throws RemoteException {
		
		System.out.println( this.getClass().getName() + ": Got single alarm." );
		
		StatefulKnowledgeSession session = sessionProvider.provide();
		
		for ( IRunningMetric metric : metrics ) {
			session.insert( metric );
		}
		
		session.insert( new Alarm( description ) );
		session.fireAllRules();
		
	}
	
	private SessionProvider sessionProvider = null;

}
